/*
  ==============================================================================

   This file is part of the JUCE library - "Jules' Utility Class Extensions"
   Copyright 2004-11 by Raw Material Software Ltd.

  ------------------------------------------------------------------------------

   JUCE can be redistributed and/or modified under the terms of the GNU General
   Public License (Version 2), as published by the Free Software Foundation.
   A copy of the license is included in the JUCE distribution, or can be found
   online at www.gnu.org/licenses.

   JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  ------------------------------------------------------------------------------

   To release a closed-source product which uses JUCE, commercial licenses are
   available: visit www.rawmaterialsoftware.com/juce for more information.

  ==============================================================================
*/

#ifndef __JUCE_D3D9TEXTURE_JUCEHEADER__
#define __JUCE_D3D9TEXTURE_JUCEHEADER__

//==============================================================================
/**
    Creates an D3D9 texture from an Image.
*/
class JUCE_API  D3D9Texture
{
public:
    D3D9Texture();
    ~D3D9Texture();

    /** Creates a texture from the given image.

        Note that if the image's dimensions aren't a power-of-two, the texture may
        be created with a larger size.

    */
    void loadImage (const Image& image);

    /** Creates a texture from a raw array of pixels.
        The width and height provided must be valid. 
        The data is sent directly to the D3D driver without being flipped vertically
    */
    void loadARGB (const PixelARGB* pixels, int width, int height);


    /** Frees the texture, if there is one. */
    void release();

    void bind() const;

    void unbind() const;

   #if JUCE_USE_D3D9_FIXED_FUNCTION
    /** Draws this texture into the current context, with the specified corner positions. */
    void draw2D (float x1, float y1,
                 float x2, float y2,
                 float x3, float y3,
                 float x4, float y4,
                 const Colour& colour) const;

    /** Draws this texture into the current context, with the specified corner positions. */
    void draw3D (float x1, float y1, float z1,
                 float x2, float y2, float z2,
                 float x3, float y3, float z3,
                 float x4, float y4, float z4,
                 const Colour& colour) const;
   #endif

    /** Returns the D3D9 texture resource. */
    LPDIRECT3DTEXTURE9 getTexture() const noexcept        { return D3Dtexture; }

    int getWidth() const noexcept               { return width; }
    int getHeight() const noexcept              { return height; }

    /** Returns true if a texture can be created with the given size.
        Some systems may require that the sizes are powers-of-two.
    */
    static bool isValidSize (int width, int height);

private:
    LPDIRECT3DTEXTURE9 D3Dtexture;
    int width, height;

    void create (int w, int h, const void*);

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (D3D9Texture);
};


#endif   // __JUCE_D3D9TEXTURE_JUCEHEADER__
